﻿package mortal.game.view.business
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.menu.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class TradeModule extends BaseWindow
    {
        private var _lock1:Boolean = false;
        private var _lock2:Boolean = false;
        private var lockEffect1:GSprite;
        private var lockEffect2:GSprite;
        private var _topContanier:GSprite;
        private var _txtTargetInfo:GTextFiled;
        private var _middleContanier:GSprite;
        private var _listBg:ScaleBitmap;
        private var _listTarget:GTileList;
        private var _listSelf:GTileList;
        private var _inputCoinTarget:MoneyInput;
        private var _inputGoldTarget:MoneyInput;
        private var _inputCoinSelf:MoneyInput;
        private var _inputGoldSelf:MoneyInput;
        private var _bottomContanier:GSprite;
        private var _btnAddFriend:GButton;
        private var _btnSendPrivate:GButton;
        private var _btnLock:GButton;
        private var _btnTrade:GButton;
        public var myItemVec:Vector.<TradeItemVo>;
        public var targetItemVec:Vector.<TradeItemVo>;
        public var targetSBusinessInfo:SBusinessInfo;
        public static const listLength:int = 6;
        public static var _selectingNumItem:ItemData;

        public function TradeModule(param1:ILayer = null)
        {
            super(param1);
            setSize(333, 500);
            title = Language.getString(21336);
            this.myItemVec = new Vector.<TradeItemVo>;
            this.targetItemVec = new Vector.<TradeItemVo>;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._listBg = UIFactory.bg(14, 35, 308, 450, this);
            var _loc_1:* = GlobalStyle.textFormatBai;
            this._topContanier = UIFactory.getUICompoment(GSprite, 20, 45, this);
            this._txtTargetInfo = UIFactory.textField(Language.getString(21337), 0, 0, 300, 20, this._topContanier, _loc_1);
            this._middleContanier = UIFactory.getUICompoment(GSprite, 17, 70, this);
            this._listTarget = UIFactory.tileList(4, 4, 148, 312, this._middleContanier);
            this._listTarget.rowHeight = 51;
            this._listTarget.columnWidth = 146;
            this._listTarget.setStyle("cellRenderer", TradeListRenderer);
            this._listTarget.rowCount = listLength;
            this._listTarget.columnCount = 1;
            this._listTarget.dataProvider = this.getMyListDp();
            this._listSelf = UIFactory.tileList(152, 4, 148, 312, this._middleContanier);
            this._listSelf.rowHeight = 51;
            this._listSelf.columnWidth = 146;
            this._listSelf.setStyle("cellRenderer", TradeMyItemListRenderer);
            this._listSelf.rowCount = listLength;
            this._listSelf.columnCount = 1;
            this._listSelf.dataProvider = this.getTargetListDp();
            this.lockEffect1 = UIFactory.getUICompoment(GSprite, 4, 4, this._middleContanier);
            this.lockEffect1.graphics.lineStyle(2, 16776960, 1);
            this.lockEffect1.graphics.beginFill(16777215, 0);
            this.lockEffect1.graphics.drawRect(0, 0, 148, 307);
            this.lockEffect1.graphics.endFill();
            this.lockEffect1.mouseEnabled = false;
            this.lockEffect1.mouseChildren = false;
            this.lockEffect1.filters = [new BlurFilter()];
            this.lockEffect1.cacheAsBitmap = true;
            this.lockEffect1.visible = false;
            this.lockEffect2 = UIFactory.getUICompoment(GSprite, 152, 4, this._middleContanier);
            this.lockEffect2.graphics.lineStyle(2, 16776960, 1);
            this.lockEffect2.graphics.beginFill(16777215, 0);
            this.lockEffect2.graphics.drawRect(0, 0, 148, 307);
            this.lockEffect2.graphics.endFill();
            this.lockEffect2.filters = [new BlurFilter()];
            this.lockEffect2.cacheAsBitmap = true;
            this.lockEffect2.visible = false;
            this._inputGoldTarget = UIFactory.getUICompoment(MoneyInput, 26, 314, this._middleContanier);
            this._inputGoldTarget.unit = EPriceUnit._EPriceUnitGold;
            this._inputGoldTarget.value = 0;
            var _loc_2:Boolean = false;
            this._inputGoldTarget.mouseEnabled = false;
            this._inputGoldTarget.mouseChildren = _loc_2;
            this._inputCoinTarget = UIFactory.getUICompoment(MoneyInput, 26, 339, this._middleContanier);
            this._inputCoinTarget.unit = EPriceUnit._EPriceUnitCoin;
            this._inputCoinTarget.value = 0;
            var _loc_2:Boolean = false;
            this._inputCoinTarget.mouseEnabled = false;
            this._inputCoinTarget.mouseChildren = _loc_2;
            this._inputGoldSelf = UIFactory.getUICompoment(MoneyInput, 170, 314, this._middleContanier);
            this._inputGoldSelf.unit = EPriceUnit._EPriceUnitGold;
            this._inputGoldSelf.defultText = Language.getString(21338);
            this._inputGoldSelf.checkSelfMoeny = true;
            this._inputCoinSelf = UIFactory.getUICompoment(MoneyInput, 170, 339, this._middleContanier);
            this._inputCoinSelf.unit = EPriceUnit._EPriceUnitCoin;
            this._inputCoinSelf.defultText = Language.getString(21338);
            this._inputCoinSelf.checkSelfMoeny = true;
            this._bottomContanier = UIFactory.getUICompoment(GSprite, 32, 448, this);
            this._btnAddFriend = UIFactory.gButton(Language.getString(21339), 15, 0, 42, 22, this._bottomContanier);
            this._btnSendPrivate = UIFactory.gButton(Language.getString(21340), 60, 0, 42, 22, this._bottomContanier);
            this._btnLock = UIFactory.gButton(Language.getString(21341), 145, 0, 66, 22, this._bottomContanier);
            this._btnTrade = UIFactory.gButton(Language.getString(21342), 215, 0, 66, 22, this._bottomContanier);
            this._btnAddFriend.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._btnSendPrivate.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._btnLock.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._btnTrade.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._inputCoinSelf.configEventListener(Event.CHANGE, this.onMoneysChange);
            this._inputGoldSelf.configEventListener(Event.CHANGE, this.onMoneysChange);
            this.configEventListener(DragEvent.Event_Move_In, this.itemMoveInHandler);
            Dispatcher.addEventListener(EventName.TradeClickItem, this.onItemClick);
            Dispatcher.addEventListener(EventName.TradeSelectNumOver, this.onSelectNunOver);
            Dispatcher.addEventListener(EventName.TradeClickPackItem, this.onClickBagItem);
            this.updateViewByData();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            Dispatcher.removeEventListener(EventName.TradeClickItem, this.onItemClick);
            Dispatcher.removeEventListener(EventName.TradeSelectNumOver, this.onSelectNunOver);
            Dispatcher.removeEventListener(EventName.TradeClickPackItem, this.onClickBagItem);
            this.lockEffect1.visible = true;
            this.lockEffect1.mouseEnabled = true;
            this.lockEffect1.mouseChildren = true;
            this.lockEffect1.cacheAsBitmap = false;
            this.lockEffect1.graphics.clear();
            this.lockEffect1.filters = null;
            this.lockEffect1.dispose(true);
            this.lockEffect2.visible = true;
            this.lockEffect2.cacheAsBitmap = false;
            this.lockEffect2.graphics.clear();
            this.lockEffect2.filters = null;
            this.lockEffect2.dispose(true);
            this._txtTargetInfo.dispose(param1);
            this._topContanier.dispose(param1);
            this._listBg.dispose(param1);
            this._listTarget.dispose(param1);
            this._listSelf.dispose(param1);
            var _loc_2:Boolean = true;
            this._inputCoinTarget.mouseEnabled = true;
            this._inputCoinTarget.mouseChildren = _loc_2;
            this._inputCoinTarget.dispose(param1);
            var _loc_2:Boolean = true;
            this._inputGoldTarget.mouseEnabled = true;
            this._inputGoldTarget.mouseChildren = _loc_2;
            this._inputGoldTarget.dispose(param1);
            var _loc_2:Boolean = true;
            this._inputCoinSelf.mouseChildren = true;
            this._inputCoinSelf.mouseEnabled = _loc_2;
            this._inputCoinSelf.dispose(param1);
            var _loc_2:Boolean = true;
            this._inputGoldSelf.mouseChildren = true;
            this._inputGoldSelf.mouseEnabled = _loc_2;
            this._inputGoldSelf.dispose(param1);
            this._middleContanier.dispose(param1);
            this._btnAddFriend.dispose(param1);
            this._btnSendPrivate.dispose(param1);
            this._btnLock.dispose(param1);
            this._btnTrade.dispose(param1);
            this._bottomContanier.dispose(param1);
            this.lockEffect1 = null;
            this.lockEffect2 = null;
            this._txtTargetInfo = null;
            this._topContanier = null;
            this._listBg = null;
            this._listTarget = null;
            this._listSelf = null;
            this._inputCoinTarget = null;
            this._inputGoldTarget = null;
            this._inputCoinSelf = null;
            this._inputGoldSelf = null;
            this._middleContanier = null;
            this._btnAddFriend = null;
            this._btnSendPrivate = null;
            this._btnLock = null;
            this._btnTrade = null;
            this._bottomContanier = null;
            super.disposeImpl(param1);
            return;
        }// end function

        override protected function closeBtnClickHandler(event:MouseEvent) : void
        {
            this.endTrading();
            super.closeBtnClickHandler(event);
            return;
        }// end function

        override public function hide() : void
        {
            this._lock1 = false;
            this._lock2 = false;
            this.myItemVec.splice(0, this.myItemVec.length);
            this.targetItemVec.splice(0, this.targetItemVec.length);
            this.targetSBusinessInfo = null;
            super.hide();
            return;
        }// end function

        private function updateViewByData() : void
        {
            var _loc_1:String = null;
            this.targetSBusinessInfo = Cache.instance.trade.getTargetSBusinessInfo();
            this._btnAddFriend.enabled = Cache.instance.friend.findRecordByRoleId(this.targetSBusinessInfo.entityId.id) == null ? (true) : (false);
            this._btnTrade.enabled = false;
            if (Cache.instance.friend.findRecordByRoleId(this.targetSBusinessInfo.entityId.id) == null)
            {
                _loc_1 = Language.getStringByParam(21344, HTMLUtil.addColor(Language.getString(21343), GlobalStyle.colorHong));
            }
            else
            {
                _loc_1 = Language.getString(21345);
            }
            _loc_1 = _loc_1 + Language.getStringByParam(21346, NameUtil.getNameByCamp(this.targetSBusinessInfo.camp, this.targetSBusinessInfo.name), this.targetSBusinessInfo.level.toString());
            _loc_1 = "<p align = \'center\'>" + _loc_1 + "</p>";
            this._txtTargetInfo.htmlText = _loc_1;
            return;
        }// end function

        private function onClickBagItem(event:DataEvent) : void
        {
            this.startPutInItem(event.data as ItemData);
            return;
        }// end function

        private function itemMoveInHandler(event:DragEvent) : void
        {
            var _loc_2:* = event.dragSouce as ItemData;
            if (_loc_2.serverData.posType == EPlayerItemPosType._EPlayerItemPosTypeBag)
            {
                this.startPutInItem(_loc_2);
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(21347));
                return;
            }
            return;
        }// end function

        private function startPutInItem(param1:ItemData) : void
        {
            if (this.lock2)
            {
                MsgManager.showRollTipsMsg(Language.getString(21348));
                return;
            }
            if (this.myItemVec.length >= TradeModule.listLength)
            {
                MsgManager.showRollTipsMsg(Language.getString(21349));
                return;
            }
            if (param1.itemAmount > 1)
            {
                _selectingNumItem = param1;
                if (TradeSelectNumWin.instance.isHide)
                {
                    TradeSelectNumWin.instance.show();
                }
                else
                {
                    TradeSelectNumWin.instance.updateViewByData();
                }
            }
            else
            {
                this.putInItem(param1, param1.itemAmount);
            }
            return;
        }// end function

        private function onSelectNunOver(event:DataEvent) : void
        {
            this.putInItem(_selectingNumItem, int(event.data));
            _selectingNumItem = null;
            return;
        }// end function

        private function putInItem(param1:ItemData, param2:int) : void
        {
            if (this.myItemVec.length >= TradeModule.listLength)
            {
                MsgManager.showRollTipsMsg(Language.getString(21349));
                return;
            }
            var _loc_3:* = new TradeItemVo(param1, param2);
            this.myItemVec.push(_loc_3);
            this._listSelf.dataProvider = this.getMyListDp();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TradeMyItemsChange, {uid:param1.uid, amount:param2}));
            return;
        }// end function

        private function putOutItem(param1:ItemData) : void
        {
            if (param1 == null)
            {
                return;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this.myItemVec.length)
            {
                
                if (this.myItemVec[_loc_2].itemData == param1)
                {
                    this.myItemVec.splice(_loc_2, 1);
                    break;
                }
                _loc_2++;
            }
            this._listSelf.dataProvider = this.getMyListDp();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TradeMyItemsChange, {uid:param1.uid, amount:0}));
            return;
        }// end function

        private function getMyListDp() : DataProvider
        {
            return new DataProvider(this.getArr(this.myItemVec));
        }// end function

        private function getTargetListDp() : DataProvider
        {
            return new DataProvider(this.getArr(this.targetItemVec));
        }// end function

        private function getArr(param1:Vector.<TradeItemVo>) : Array
        {
            var _loc_2:Array = [];
            var _loc_3:int = 0;
            while (_loc_3 < listLength)
            {
                
                if (param1 && param1.length > _loc_3)
                {
                    _loc_2.push(param1[_loc_3]);
                }
                else
                {
                    _loc_2.push(new Object());
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        private function onItemClick(event:DataEvent) : void
        {
            if (this.lock2)
            {
                MsgManager.showRollTipsMsg(Language.getString(21348));
                return;
            }
            this.putOutItem(event.data as ItemData);
            return;
        }// end function

        private function onMoneysChange(event:Event) : void
        {
            var _loc_2:* = event.currentTarget as MoneyInput;
            var _loc_3:Object = {};
            _loc_3.unit = _loc_2.unit;
            _loc_3.amount = _loc_2.value;
            Dispatcher.dispatchEvent(new DataEvent(EventName.TradeMyMoneysChange, _loc_3));
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            if (event.currentTarget == this._btnAddFriend)
            {
                this.addFriend();
            }
            else if (event.currentTarget == this._btnSendPrivate)
            {
                this.sendPrivate();
            }
            else if (event.currentTarget == this._btnLock)
            {
                this.lock();
            }
            else if (event.currentTarget == this._btnTrade)
            {
                this.clickTrade();
            }
            return;
        }// end function

        private function addFriend() : void
        {
            PlayerMenuConst.Opearte(PlayerMenuConst.AddFriend, this.createMiniPlayer());
            return;
        }// end function

        private function sendPrivate() : void
        {
            PlayerMenuConst.Opearte(PlayerMenuConst.ChatPirvate, this.createMiniPlayer());
            return;
        }// end function

        private function createMiniPlayer() : SMiniPlayer
        {
            var _loc_1:* = new SMiniPlayer();
            var _loc_2:* = this.targetSBusinessInfo;
            _loc_1.entityId = _loc_2.entityId;
            _loc_1.name = _loc_2.name;
            return _loc_1;
        }// end function

        private function lock() : void
        {
            var onSelect:Function;
            var str:String;
            onSelect = function (param1:int) : void
            {
                if (param1 == Alert.OK)
                {
                    lock2 = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TradeClickLock));
                }
                return;
            }// end function
            ;
            if (this._inputGoldSelf.value > TradeCache.goldRemind)
            {
                str = Language.getString(21350);
                Alert.show(str, null, Alert.OK | Alert.CANCEL, null, onSelect);
            }
            else
            {
                this.onSelect(Alert.OK);
            }
            return;
        }// end function

        private function clickTrade() : void
        {
            this._btnTrade.enabled = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.TradeClickTradeBtn));
            return;
        }// end function

        private function endTrading() : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.TradeCancel));
            return;
        }// end function

        public function get lock1() : Boolean
        {
            return this._lock1;
        }// end function

        public function set lock1(param1:Boolean) : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this._lock1 = param1;
            this.lockEffect1.visible = param1;
            this._btnTrade.enabled = this._lock1 && this._lock2;
            return;
        }// end function

        public function get lock2() : Boolean
        {
            return this._lock2;
        }// end function

        public function set lock2(param1:Boolean) : void
        {
            this._lock2 = param1;
            this.lockEffect2.visible = param1;
            var _loc_2:* = !this._lock2;
            this._inputCoinSelf.mouseChildren = !this._lock2;
            this._inputCoinSelf.mouseEnabled = _loc_2;
            var _loc_2:* = !this._lock2;
            this._inputGoldSelf.mouseChildren = !this._lock2;
            this._inputGoldSelf.mouseEnabled = _loc_2;
            this._btnLock.enabled = !this._lock2;
            this._btnTrade.enabled = this._lock1 && this._lock2;
            return;
        }// end function

        public function updateTargetItem(param1:Dictionary) : void
        {
            var _loc_3:SBusinessItemUpdate = null;
            var _loc_5:ItemData = null;
            var _loc_6:TradeItemVo = null;
            this.targetItemVec.splice(0, this.targetItemVec.length);
            var _loc_2:* = this.sortItemDicAsArr(param1);
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_4];
                if (_loc_3.updateType.value() != EUpdateType._EUpdateTypeDel)
                {
                    _loc_5 = new ItemData(_loc_3.playerItem);
                    _loc_6 = new TradeItemVo(_loc_5, _loc_5.itemAmount);
                    this.targetItemVec.push(_loc_6);
                }
                _loc_4++;
            }
            this._listTarget.dataProvider = this.getTargetListDp();
            return;
        }// end function

        private function sortItemDicAsArr(param1:Dictionary) : Array
        {
            var _loc_3:SBusinessItemUpdate = null;
            var _loc_2:Array = [];
            for each (_loc_3 in param1)
            {
                
                _loc_2.push(_loc_3);
            }
            _loc_2.sortOn("index", Array.NUMERIC);
            return _loc_2;
        }// end function

        public function updateTargetMoneys(param1:int, param2:int) : void
        {
            this._inputCoinTarget.value = param2;
            this._inputGoldTarget.value = param1;
            return;
        }// end function

    }
}
